package com.qf.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Java发送http请求的工具
 * 集成OKHttp框架
 * java发送http请求可以有多种实现：
 * HttpUrlConnection
 * HttpClient
 * OKHttp
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.04.09
 * @since 1.0
 */
public class HttpUtil {
    /**
     * MIME类型
     */
    private static final MediaType MEDIA_TYPE_JSON= MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient okHttpClient;
    /**
     * 私有化构造方法
     */
    private HttpUtil(){
        okHttpClient=new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .build();
    }

    private static class HttpUtilHolder{
        private static HttpUtil instance=new HttpUtil();
    }

    public static HttpUtil getInstance(){
        return HttpUtilHolder.instance;
    }

    /**
     * http的post请求
     */
    public String doPost(String url,String json){
        //请求报文的请求体
        RequestBody body=RequestBody.create(MEDIA_TYPE_JSON,json);
        Request request=null;
        Request.Builder builder=new Request.Builder().url(url).post(body);
        //.addHeader("token","asldkjflajsdfl");
        //实例请求对象
        request=builder.build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String doPost(String url,String json,String token){
        //请求报文的请求体
        RequestBody body=RequestBody.create(MEDIA_TYPE_JSON,json);
        Request request=null;
        Request.Builder builder=new Request.Builder().url(url).post(body)
                .addHeader("token",token);
        //实例请求对象
        request=builder.build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        //main方法当成postman用
        String url="http://localhost:9001/qf/user/signIn";

        //转义字符  \"代表一个"
        //转义字符  \\代表一个\
        //String temp="aksdjfl\"jal\\sdkjf";
        //System.out.println(temp);

        String json="{\"signInName\":\"zhangsan\",\"password\":\"123456\"}";

        String responseText = getInstance().doPost(url, json);
        JSONObject res=JSONObject.parseObject(responseText);
        String token=res.getJSONObject("result").getString("token");

        //带着token，请求http://localhost:9001/qf/user/test
        //如果服务器能认识我，就说明，我成功携带身份了。
        String text = getInstance().doPost("http://localhost:9001/qf/user/test", "{}", token);

        System.out.println(text);
    }


}